import numpy as np

N = 1000 
e = np.random.normal(0, 1, N) 
x = np.random.normal(0, 1, [N, 3]) 
x[:,0] = 1  

w_real = np.array([4, 3, 2]) #求W

#DGP: y = 4 + 3*x0 + 2*x1 + error 
y = np.matmul(x, w_real) + e 

import AutoDiff as AD 

m = AD.Model()
m.add_layer(AD.Linear, [3, 3])
m.add_layer(AD.Linear, 3) 
m.add_loss(AD.MSE)

m.train(x, y) 

m1 = AD.Model() 
m1.add_layer(AD.Linear, 3) 
m1.add_loss(AD.MSE) 

m1.train(x, y)

xtx_inv = np.linalg.inv(np.matmul(np.transpose(x), x))
xty = np.matmul(np.transpose(x), y) 
w_linreg = np.matmul(xtx_inv, xty) 

w0 = np.matmul(m.w_sequence[0].val, m.w_sequence[1].val)

print("Estimated weight {}".format(np.round(m1.w_sequence[0].val, 4))) 
print("Estimated weight hidden layer {}".format(np.round(w0, 4))) 
print("Lin Reg Weight: {}".format(w_linreg.round(4))) 
print("Real weight {}".format(w_real)) 
#print("Difference {}".format(np.round(w_real - m.w_sequence[0].val, 4))) 

